package com.framework.wz.wutong.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.framework.wz.wutong.R;
import com.framework.wz.wutong.base.BaseActivity;
import com.framework.wz.wutong.utils.ToastUtil;
import com.shumai.liveness.LivenessMainActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LiveCompareActivity extends BaseActivity {

    @BindView(R.id.et_input1)
    EditText etInput1;
    @BindView(R.id.et_input2)
    EditText etInput2;
    @BindView(R.id.btn_jc)
    Button btnJc;

    private static final int START_LIVE_DETECT = 99;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_live_compare;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        btnJc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(etInput1.getText().toString().trim())) {
                    ToastUtil.show(LiveCompareActivity.this, "请输入身份证号");
                    return;
                }
                if (TextUtils.isEmpty(etInput2.getText().toString().trim())) {
                    ToastUtil.show(LiveCompareActivity.this, "请输入姓名");
                    return;
                }
                Intent intent = new Intent(LiveCompareActivity.this,
                        LivenessMainActivity.class);
                Bundle bundle = new Bundle();

                bundle.putString("actions", "1279");
                bundle.putString("actionsNum", "3");

//
                JSONObject obj = new JSONObject();


                obj.put("liveType", "2"); // 1 活体 2 活体加人像比对
                obj.put("idcard", etInput1.getText().toString().trim());
                obj.put("name", etInput2.getText().toString().trim());

                bundle.putString("bizData", obj.toJSONString());

                intent.putExtra("liveness", bundle);

//                intent.setFlags(FLAG_ACTIVITY_NEW_TASK);

                startActivityForResult(intent, START_LIVE_DETECT);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == START_LIVE_DETECT) {
            switch (resultCode) { //resultCode为回传的标记，RESULT_OK
                case RESULT_OK:
                    if (null != intent) {
                        Bundle result = intent.getBundleExtra("result");
                        // 错误码, 0检测成功
                        String code = result.getString("code");
                        // 错误信息
                        String msg = result.getString("msg");
                        // 检测通过,code="0"时返回图片保存路径，完整图像
                        String passImgPath = result.getString("passImgPath");
                        // 检测通过,code="0"时返脸部base64，剪裁后的脸部图像
                        String passFace = result.getString("passFace");
                        String data = result.getString("data");
                        Log.e("189", code + " : "+ msg+", data:"+data);
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
